angular.module('app.goods', [
])
.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('home.goodslist', {
            url: '/goods/list/:shopid?:name',
            templateUrl: "tpls/goods/list.html",
            resolve: {
                catagory_list : ['CatagoryService', function(CatagoryService){
                    return CatagoryService.getlist().$promise;
                }]
            },
            controller: 'GoodsController'
        })
        .state('home.goodsimport', {
            url: '/good/import',
            templateUrl: "tpls/goods/import.html",
            resolve: {
                catagory_list : ['CatagoryService', function(CatagoryService){
                    return CatagoryService.getlist().$promise;
                }]
            },
            controller: 'GoodsImportController'
        })
}])

.controller('GoodsController', ['$scope','$stateParams','GoodService','CatagoryService', function($scope, $stateParams, GoodService, CatagoryService) {
    $scope.goodslist = [];
    //console.log($stateParams);
    if ($stateParams.shopid) {
        GoodService.getshopgoodslist($stateParams.shopid).$promise.then(function(goodslist){
            for (var i=0; i<goodslist.length; i++) {
                var goods = goodslist[i];
                goods.shopname = $stateParams.name;
                goods.cataname = CatagoryService.getcatanamebyid(goods.cataid);
            }
            //
            $scope.goodslist = goodslist;
        });
    }
    //
    $scope.remove = function(goodsid) {
        //console.log(shopid);
        // 只删除引用，并不真正删除店铺
        GoodService.removegoodbyid(goodsid).$promise.then(function(data){
            window.location.reload();
        });
    }
}])

.controller('GoodsImportController', ['$scope','GoodService','ShopService','CatagoryService', function($scope, GoodService, ShopService, CatagoryService) {
    $(function () {
        $('#doc-form-file').on('change', function () {
            var fileNames = '';
            $.each(this.files, function () {
                fileNames += '<span class="am-badge">' + this.name + '</span> ';
            });
            $('#file-list').html(fileNames);
        });
    });

        $scope.alert_msg = "";
        $scope.enable_submit = false;
        $scope.close_alert = function() {
            $scope.alert_msg = "";
        }

  $scope.readFile = function () {
        //读取回调
        function process_wb(wb) {
            var workbook = ExcelImporter.to_json(wb);
            var shop_map = {};
            $scope.goods = new Array();
            $scope.$apply(function () {
                $scope.rows = workbook['Sheet1'];
                for (var i = 0; i < $scope.rows.length; i++) {
                    var good = {};
                    good.shopname = $scope.rows[i].商店名称;
                    good.name = $scope.rows[i].商品名称;
                    good.cataname = $scope.rows[i].商品分类;
                    good.unit = $scope.rows[i].商品单位;
                    if(!good.unit){
                        good.unit="斤";
                    }
                    //good.cataid = $filter('cata_type')(good.cataname);
                    good.cataid = CatagoryService.getcataidbyname(good.cataname);
                    good.pic = $scope.rows[i].商品图片;
                    good.price = $scope.rows[i].商品价格;
                    good.stock = $scope.rows[i].商品库存量;
                    good.description = $scope.rows[i].商品描述;
                    $scope.goods.push(good);
                }

                check_cataid($scope.goods, 0);
                function check_cataid(goods_list, i) {
                    if (i >= goods_list.length) {
                        console.log($scope.goods);
                        prepare_shopid($scope.goods, 0);
                        return;
                    }
                    var goods = goods_list[i];
                    if(goods.cataid){
                        check_cataid(goods_list, i+1);
                    }else{
                        var n=i+1;
                        $scope.alert_msg = "第"+n+"条数据商品分类不存在，请检查数据！";
                        return;
                    }
                }
                function prepare_shopid(goods_list, i) {
                    if (i >= goods_list.length) {
                        $scope.enable_submit = true;
                        return;
                    }
                    var goods = goods_list[i];
                    if (shop_map[goods.shopname]) {
                        goods.shopid = shop_map[goods.shopname];
                        prepare_shopid(goods_list, i + 1);
                    } else {
                        ShopService.getshopbyname(goods.shopname).$promise.then(function (data) {
                            //console.log(data);
                            if(data.shopid==undefined){
                                $scope.alert_msg = "商店不存在，请检查数据！";
                                return;
                            }
                            goods.shopid = data.shopid;
                            shop_map[goods.shopname] = data.shopid;
                            prepare_shopid(goods_list, i + 1);
                        });
                    }
                }

            });
        }

        var target = document.getElementById('doc-form-file');
        var files = target.files;
        var f = files[0];
        ExcelImporter.importexcel(f, process_wb);
    }
    $scope.submit = function () {
        //console.log($scope.goods);
        addgoods($scope.goods, 0);

        function addgoods(goods_list, i) {
            if (i >= goods_list.length) {
                alert("导入成功！");
                window.location.reload();
                return;
            }
            var goods = goods_list[i];
            GoodService.addgood(goods, function (data) {
                addgoods(goods_list, i + 1);
            }, function (data) {
                alert("导入失败！");
                return;
            });
        }
    }
}])
;